package com.hpin.fansea.service;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.CommentAdapter;
import com.hpin.fansea.adapter.PictureAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.CommentList;
import com.hpin.fansea.bean.HouseDetailBean;
import com.hpin.fansea.pulltorefush.PullToRefreshBase;
import com.hpin.fansea.pulltorefush.PullToRefreshListView;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.lidroid.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 出租详情
 * Created by Aaron on 15/9/17.
 */
public class RentDetailActivity extends BaseActivity {

    private PullToRefreshListView plv_house_detail;
    private TextView tv_price;
    private TextView tv_contact;
    private TextView tv_contact_tel;
    private TextView tv_address;
    private TextView tv_detail;
    private TextView tv_sale;
    private ViewPager vp_banner;

    private List<CommentList> commentLists = new ArrayList<CommentList>();
    private CommentAdapter mAdapter;

    //房屋id
    private String houseRentId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_house_detail);

        houseRentId = getIntent().getStringExtra("houseRentId");

        initWidget();

        getHouseDetailInfo();

    }

    /**
     * 获取房屋详情
     */
    private void getHouseDetailInfo() {

        RequestParams params = new RequestParams();
//        params.addQueryStringParameter("houseRentId", houseRentId);
        HttpUtil.getInstance().setGetRequest("获取中...", this, Constant.WEB_ROOT + Constant.HOUSEDETAILINFO
                        + "?token=" + sp.getString(Constant.token, "") + "&houseRentId=" + houseRentId, params,
                new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            HouseDetailBean detailBean = JSONObject.parseObject(str, HouseDetailBean.class);

                            if (Constant.STATUS_SUCESS.equals(detailBean.status)) {

                                tv_price.setText(detailBean.result.price);
                                tv_contact.setText(detailBean.result.contactUser);
                                tv_contact_tel.setText(detailBean.result.contactUserPhone);
                                tv_address.setText(detailBean.result.detailAddress);
                                tv_detail.setText(detailBean.result.desc);
                                tv_sale.setText(detailBean.result.title);

                                PictureAdapter pictureAdapter = new PictureAdapter(detailBean.result.pics, RentDetailActivity.this);
                                vp_banner.setAdapter(pictureAdapter);

                            }else {
                                showToast(detailBean.message);
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast(Constant.NODATA);
                        }

                    }
                }, null);

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);

//        tv_right.setText(getResources().getString(R.string.comment));

        tv_right.setOnClickListener(this);

        tv_title.setText(getResources().getString(R.string.house_rent_detail));

        iv_left.setOnClickListener(this);

        plv_house_detail = (PullToRefreshListView) findViewById(R.id.plv_house_detail);

        View view = View.inflate(this, R.layout.head_house_detail, null);

        plv_house_detail.getRefreshableView().addHeaderView(view);

        vp_banner = (ViewPager) view.findViewById(R.id.vp_banner);

        TextView tv_sale_rent = (TextView) findViewById(R.id.tv_sale_rent);
        tv_sale_rent.setText(getResources().getString(R.string.rent_space));

        tv_sale = (TextView) findViewById(R.id.tv_sale);

        TextView tv_price_sale_rent = (TextView) findViewById(R.id.tv_price_sale_rent);
        tv_price_sale_rent.setText(getResources().getString(R.string.rent_price));

        tv_price = (TextView) findViewById(R.id.tv_price);
        tv_contact = (TextView) findViewById(R.id.tv_contact);
        tv_contact_tel = (TextView) findViewById(R.id.tv_contact_tel);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_detail = (TextView) findViewById(R.id.tv_detail);

        plv_house_detail.setMode(PullToRefreshBase.Mode.DISABLED);

        mAdapter = new CommentAdapter(this, commentLists);
        plv_house_detail.setAdapter(mAdapter);


        plv_house_detail.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                String label = DateUtils.formatDateTime(RentDetailActivity.this, System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);


            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                String label = DateUtils.formatDateTime(RentDetailActivity.this, System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);


            }
        });

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;

//            case R.id.tv_right://评论
//
//                // 游客权限控制
//                if (Constant.STATUS_VISITOR.equals(sp.getString(Constant.status, ""))) {
//                    remindLogin("亲,登录后才可以评论哦");
//                    return;
//                }
//
//                Intent mIntent = new Intent(this, HouseCommentActivity.class);
//                startActivity(mIntent);
//
//                break;

        }

    }
}
